package com.dcjt.firehotcarpad.fragment

import MapOperationEventPool
import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProviders
import com.aries.library.fast.manager.LoggerManager
import com.dcjt.firehotcarpad.R
import com.dcjt.firehotcarpad.model.HandleDisasterViewModel
import com.dcjt.firehotcarpad.utils.AlertDialogUtil
import com.dcjt.firehotcarpad.utils.MarkerToMapType
import com.flyco.tablayout.listener.OnTabSelectListener
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity
import com.yhao.floatwindow.FloatWindow

/**
 * Created by cj on 2021/2/20.
 * Email:codesexy@163.com
 * Function:
 * desc:处警页面的左边的Fragment
 */
class HandlerOperationFragment : BaseTabsFragment() {

    companion object {
        fun newInstance(zqid:String): HandlerOperationFragment {
            var fragment = HandlerOperationFragment()
            val bundle = Bundle()
            bundle.putString("zqid", zqid)
            fragment.setArguments(bundle)
            return fragment
        }
    }
    @SuppressLint("UseRequireInsteadOfGet")
    override fun beforeInitView() {
        zqid = arguments?.getString("zqid").toString()
        super.beforeInitView()
    }
    private var zqid: String = ""

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        bottomContainer.visibility = View.GONE
        val floatView = FloatWindow.get().view
        val viewModel =
            ViewModelProviders.of(requireActivity()).get(HandleDisasterViewModel::class.java)
        floatView.setOnClickListener {
            AlertDialogUtil.addDisasterDocument(
                requireActivity() as RxAppCompatActivity,
                viewModel.getJqid(),
                viewModel.getTemplates()
            )
        }
        tabLayoutSliding?.setOnTabSelectListener(object : OnTabSelectListener {
            override fun onTabSelect(position: Int) {
                LoggerManager.e("tabLayoutSliding: onTabSelect:"+position)
                when(position){
                    //显示警情列表
                    0-> {
                        MapOperationEventPool.get().sendAroundWaterMakersDispatch(null, MarkerToMapType.MARKER_HINT)
                    }
                    //显示水源
                    1-> {
                        MapOperationEventPool.get().sendAroundWaterMakersDispatch(null, MarkerToMapType.MARKER_SHOW)
                    }
                }

            }

            override fun onTabReselect(position: Int) {
            }
        })

    }

    override fun getTabTitles(): List<String> =
        mContext.resources.getStringArray(R.array.arrays_title).toList()

    override fun getFragments(): List<Fragment> = mutableListOf(
        DisasterInfoFragment.newInstance(),
        WaterSourceFragment.newInstance(2),
//        WaterTabFragment.newInstance(zqid),
        FreeDocumentFragment(),
        InstructionListFragment.newInstance(zqid)
    )

    override fun onPageChange(index: Int) {
        if (index == 2) FloatWindow.get().show()
        else FloatWindow.get().hide()
    }

    override fun onResume() {
        super.onResume()
        if (FloatWindow.get().isShowing && currentIndex != 2)
            FloatWindow.get().hide()

    }
}